import { expect } from '@jest/globals';
import path from 'path';

import { runHvigorAsync } from '../../common/common';
import { addOhPackageDps, commonJson5Operation } from '../../plugins/common-fun';
import {
  AbilityType,
  APIType,
  APIVersion,
  CaseLevel,
  LanguageType,
  ModuleTemplateType,
  RuntimeOSType,
  SDKType
} from '../../plugins/project-builder/models/constants';
import { TestCaseBuilder } from '../../plugins/task-builder/task-case-builder';
import { ProjectBuildOption, TestCaseFun } from '../../plugins/task-builder/test-case';
import { TestTaskOptionBuilder } from '../../plugins/task-builder/test-task-option';

const projectBuilderOption: ProjectBuildOption = {
  template: {
    sdk: SDKType.Single,
    runtimeOS: RuntimeOSType.HarmonyOS,
    apiVersion: APIVersion.API10,
    apiType: APIType.STAGE,
    language: LanguageType.ArkTs,
    abilityType: AbilityType.Application,
    entryTemplateType: ModuleTemplateType.EmptyAbility
  },
  modulesOption: [ModuleTemplateType.SharedLibrary]
};

/**
 *  entry依赖hsp，entry的build-profile.json5中添加targets，同步编译构建报错。
 *
 * @param {Project} project
 * @param {TestTaskOption} taskConfig
 * @param {ICaseRecord} record
 * @return res
 */
const testfun: TestCaseFun = async (project, taskConfig, record) => {
  // 1. 创建API10 stage 工程 ，添加sharelibrary模块，在entry的oh-package.json5文件中配置hsp依赖
  const entryPath = project.getModule('entry').moduleRootPath();
  await addOhPackageDps(entryPath, { 'shareLibrary': '../shareLibrary' });

  // 2. entry的build-profile.json5中添加targets
  await commonJson5Operation(path.resolve(entryPath, 'build-profile.json5'), async (json5) => {
    json5.targets.push({ 'name': 'test' });
    json5.targets.push({ 'name': 'test1' });
  });

  // 3. 执行同步编译构建
  const commands: string[] = '--sync -p product=default --parallel --incremental --no-daemon'.split(' ');
  const result = await runHvigorAsync(project.projectRootPath(), commands);
  record.addStep('执行同步编译构建', result);
  expect(result.stdout).toContain('Finished ::init');
};

TestCaseBuilder.builder()
  .caseName('DTS2023052703766')
  .description('【DevEco Studio】【HOS单框架】【API10】【修改引入】【win/mac】entry依赖hsp，entry的build-profile.json5中添加targets，同步编译构建报错。')
  .projectBuilderOption(projectBuilderOption)
  .taskConfig(TestTaskOptionBuilder.builder().caseLevel(CaseLevel.DTS).build())
  .testFun(testfun)
  .load();
